package com.dman.multi.cmd;

import org.apache.pivot.collections.List;
import org.apache.pivot.wtk.BoxPane;
import org.apache.pivot.wtk.TextInput;
import org.apache.pivot.wtk.TextInputContentListener;

import com.dman.multi.core.Download;

public class GUISearchCommand implements Command {
	
	private List<Download> downloadList;
	private TextInput textInput;
	private BoxPane boxPane;

	public GUISearchCommand(BoxPane boxPane, TextInput textInput, List<Download> downloadList) {
		this.textInput = textInput;
		this.downloadList = downloadList;
		this.boxPane = boxPane;
		this.textInput.getTextInputContentListeners().add(new TextInputContentListener.Adapter(){
			@Override
			public void textChanged(TextInput textInput) {
				String regex = textInput.getText();
				synchronized (GUISearchCommand.this.downloadList) {
					for(Download d: GUISearchCommand.this.downloadList){
						d.setVisible(false);
						if(d.getFileName().indexOf(regex) >= 0){
							d.setVisible(true);
						}
					}
				}
			}
		});
	}

	@Override
	public void execute() {
		boxPane.setVisible(true);
	}
}
